
# 编译选项(-Wall: 显示所有警告信息，-Wextra: 显示额外的警告信息)
# -g 表示编译器将生成调试信息
CFLAGS = -Wall -Wextra -I include -I ./libs


# 目标文件目录
OBJ_DIR = ./build/obj
BIN_DIR = ./bin


# 默认目标
all: help

# 编译目标
.PHONY: build
build: clean
	@echo "Building tests..."
	@mkdir -p $(BIN_DIR)
	@if [ ! -f ./bin/cJSON.o ]; then gcc -I ./libs -c ./libs/cJSON.c -o ./bin/cJSON.o; fi
	
	@gcc -I ./libs -I include -c ./src/main.c -o ./bin/main.o
	@gcc ./bin/cJSON.o ./bin/main.o -o ./bin/main.exe

	@gcc -I ./libs -I include -c ./src/config.c -o ./bin/config.o
	@gcc ./bin/cJSON.o ./bin/config.o -o ./bin/config.exe
	
	@g++ -I ./libs -I include -std=c++11  -c ./src/config.cpp -o ./bin/config.o
	@g++ ./bin/cJSON.o ./bin/config.o -o ./bin/config_cpp.exe

	@gcc -I ./libs -I include -c ./src/example.c -o ./bin/example.o
	@gcc ./bin/cJSON.o ./bin/example.o -o ./bin/example.exe

	@g++ -I ./libs -I include -std=c++11 -c ./src/example.cpp -o ./bin/example.o
	@g++ ./bin/cJSON.o ./bin/example.o -o ./bin/example_cpp.exe

	@g++ -I libs -std=c++11 -c ./src/configV2.cpp -o ./bin/config_v2.o
	@g++ ./bin/config_v2.o ./bin/cJSON.o  -o ./bin/config_v2.exe

	@find ./bin -type f -name '*.o' ! -name 'cJSON*.o' -exec rm -f {} +


# 生成测试
tests:
	@echo "Building tests..."
	@mkdir -p $(BIN_DIR)
	@if [ ! -f ./bin/cJSON.o ]; then gcc -I ./libs -c ./libs/cJSON.c -o ./bin/cJSON.o; fi
	@if [ ! -f ./bin/cJSON_Utils.o ]; then gcc -I ./libs -c ./libs/cJSON_Utils.c -o ./bin/cJSON_Utils.o; fi
	@gcc -I ./libs -c ./tests/create_test.c -o ./bin/test_create.o
	@gcc -I ./libs -c ./tests/parse_test.c -o ./bin/test_parse.o
	@gcc -I ./libs -c ./tests/utils_merge.c -o ./bin/test_utils_merge.o
	@gcc -I ./libs -c ./tests/utils_specific.c -o ./bin/test_utils_specific.o
	@gcc ./bin/cJSON.o ./bin/test_create.o -o ./bin/test_create.exe
	@gcc ./bin/cJSON.o ./bin/test_parse.o -o ./bin/test_parse.exe
	@gcc ./bin/cJSON.o ./bin/cJSON_Utils.o ./bin/test_utils_merge.o -o ./bin/test_utils_merge.exe
	@gcc ./bin/cJSON.o ./bin/cJSON_Utils.o ./bin/test_utils_specific.o -o ./bin/test_utils_specific.exe
	@rm -f ./bin/test_create.o ./bin/test_parse.o ./bin/test_utils_merge.o ./bin/test_utils_specific.o

# 清理生成的文件(保留cJSON.o和cJSON_Utils.o)
clean:
	@find ./bin -type f ! -name 'cJSON*' -exec rm -f {} +

# 打印帮助信息
help:
	@echo "Usage: make [target]"
	@echo ""
	@echo "Targets:"
	@echo "  build      编译项目"
	@echo "  clean      清理生成的文件"
	@echo "  tests      生成测试"
	@echo "  help       显示帮助信息\n"

# 伪目标,不需要生成文件
.PHONY: all build run clean help tests
